package com.zhongxi.guoyuan.fragment;


import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.ImageBannerAdapter;
import com.zhongxi.guoyuan.adapter.beautifulStore.BeautifulProjectAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.EventMessage;
import com.zhongxi.guoyuan.entity.ad.AdData;
import com.zhongxi.guoyuan.entity.ad.AdResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.Constants;
import com.zhongxi.guoyuan.utils.DeviceUtils;
import com.zhongxi.guoyuan.utils.ImageUtils;
import com.zhongxi.guoyuan.widget.CircleIndicator;
import com.zhongxi.guoyuan.widget.ViewFlow;
import com.zhongxi.guoyuan.widget.sliding.CustomViewPager;
import com.zhongxi.guoyuan.widget.view.MyScrollView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.zhongxi.guoyuan.R.id.rl_content;

public class NewMainStoreFragment extends BaseFragment implements View.OnClickListener, MyScrollView.OnScrollListener {

    @Bind(R.id.ivBack)
    ImageView ivBack;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.iv_service)
    ImageView ivService;
    @Bind(R.id.iv_dot)
    ImageView ivDot;
    @Bind(rl_content)
    RelativeLayout rlContent;
    @Bind(R.id.title)
    RelativeLayout title;
    @Bind(R.id.viewflow)
    ViewFlow viewflow;
    @Bind(R.id.viewflowdic)
    CircleIndicator viewflowdic;
    @Bind(R.id.flow)
    FrameLayout flow;
    @Bind(R.id.ll_free)
    LinearLayout llFree;
    @Bind(R.id.ll_nocard)
    LinearLayout llNocard;
    @Bind(R.id.ll_nomarket)
    LinearLayout llNomarket;
    @Bind(R.id.ll_goodcharacter)
    LinearLayout llGoodcharacter;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.iv_search)
    ImageView ivSearch;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    CustomViewPager viewPager;
    @Bind(R.id.myScrollView)
    MyScrollView myScrollView;

    private ProgressDialog mDialog;
    private PopupWindow pw;
    private int count;
    public static final int requestFulsh = 500;
    public static final int requestFulshConver = 600;
    private ImageBannerAdapter mBannerAdapter;
    private String trim = "";
    private BeautifulProjectAdapter projectAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_store, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitle();
        tvTitle.setText("美豆美容");
        //设置轮播图片高度
        ImageUtils.setHeight(getActivity(), Constants.flowRate, flow);
    }

    private void lunbo(List<AdData> data) {
        //初始化轮播控件
        viewflow.setFlowIndicator(viewflowdic);
        if (data.size() > 0) {
            viewflow.setSideBuffer(data.size()); // 实际图片数量
        } else {
            viewflow.setSideBuffer(1); // 实际图片数量
        }
        if (mBannerAdapter == null) {
            mBannerAdapter = new ImageBannerAdapter(getActivity(), data,3);
        } else {
            mBannerAdapter.replaceAll(data);
        }
        viewflow.setAdapter(mBannerAdapter);
        viewflow.startAutoFlowTimer();
    }

    @Override
    protected void initData() {
        super.initData();
        projectAdapter = new BeautifulProjectAdapter(getChildFragmentManager());
//        projectAdapter.setPrimary(trim);
        viewPager.setAdapter(projectAdapter);
        tabLayout.setupWithViewPager(viewPager);

//        adapter = new ProjectAdapter(context, list);
//        lvList.setAdapter(adapter);
        getAds();
        //获取广告轮播
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        ivSearch.setOnClickListener(this);
        rlContent.setOnClickListener(this);
        myScrollView.setOnScrollListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                getActivity().finish();
                break;
            case R.id.iv_search:
                if (TextUtils.isEmpty(etSearch.getText().toString().trim())) {
                    trim = "";
                } else {
                    trim = etSearch.getText().toString().trim();
                }
                EventBus.getDefault().post(new EventMessage(trim));
//                projectAdapter.setPrimary(trim);
//                projectAdapter.notifyDataSetChanged();

                break;
            case rl_content:
                if (CommonTool.checkApkExist(getContext(),"com.tencent.mobileqq")){
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                            "mqqwpa://im/chat?chat_type=wpa&uin="+Constants.QQ_NUM+"&version=1")));
                }else {
                    showShortToast("未安装QQ");
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        try {
            Gson gson = new Gson();
            AdResult adResult = gson.fromJson(response, AdResult.class);
            MyApplication.getInstance().setSpValue(adResult.getSafeobj());
            lunbo(adResult.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取广告展示
     */
    private void getAds() {
        String getStr = "&type=2";
        getServer(Url.SHOW_ADS, null, getStr, false, null, "get");
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == requestFulsh) {
            pw.dismiss();
        } else if (requestCode == requestFulshConver) {
            pw.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onScroll(int scrollY) {
    }

    @Override
    public void scrollBottom() {
    }
}